################################################################################
# PowerShell OnTAP Library
# License: http://www.codeplex.com/PowerShellOnTAP/license
################################################################################
###
### PowerShell OnTAP System Documentation:
###
################################################################################
$PowerShellOnTAP_System = $MyInvocation.MyCommand.Path
. (Join-Path (Split-Path $PowerShellOnTAP_System -parent) "NetApp.Management.ps1")

################################################################################
### ----------------------------------------------------------------------------
### Get-NaSystemHelp
###   Displays the help documentation for the PowerShell OnTAP System library.
###
###   Example: 
###            Get-NaSystemHelp
###
################################################################################
function Get-NaSystemHelp {
    gc $PowerShellOnTAP_System |? { $_ -match "^###( |$)" } |% { $_ -replace "^### ?","" } | more
}

################################################################################
### ----------------------------------------------------------------------------
### Get-NaSystemApiElements -server [NaServer] -api [string[]]
###   Returns the input/output elements for the specified APIs.
###
###   Example:
###            $server = New-NaServer -server "TOASTER" -credentials (Get-Credential)
###            (Get-NaSystemApiElements -server $server -api "system-api-get-elements").Elements
###
################################################################################
function Get-NaSystemApiElements {
    param (
        [PSObject] $server = $(throw "The parameter -server is required."),
        [string]   $api    = $(throw "The parameter -api is required.")
    )
    
    $apiListInfo = $api |% { "<api-list-info>{0}</api-list-info>" -f $_ }
    $request = New-NaRequest -name "system-api-get-elements"
    $request.Parameters += New-NaRequestParameter -name "api-list" -value ([string]::Concat($apiListInfo))
    
    $result = Get-NaResult -server $server -request $request
    
    if (!(Test-NaResult -result $result)) {
        Throw-NaResultError -result $result
    }
    
    return $result."api-entries"."system-api-entry-info" |% {
        New-NaObject -properties @{
            Name     = $_.name;
            Elements = $_."api-elements"."system-api-element-info" |% { ConvertTo-NaSystemApiElementInfo $_ }
        }
    }
}

################################################################################
### ----------------------------------------------------------------------------
### Get-NaSystemAPIs -server [NaServer]
###   Returns a list of APIs supported by the server.
###
###   Example:
###            $server = New-NaServer -server "TOASTER" -credentials (Get-Credential)
###            Get-NaSystemAPIs -server $server
###
################################################################################
function Get-NaSystemAPIs {
    param (
        [PSObject] $server = $(throw "The parameter -server is required.")
    )
    
    $request = New-NaRequest -name "system-api-list"
    $result  = Get-NaResult -server $server -request $request
    
    if (!(Test-NaResult -result $result)) {
        Throw-NaResultError -result $result
    }
    
    return $result.apis."system-api-info" |% {
        New-NaObject -properties @{
            Name        = $_.name;
            License     = $_.license;
            IsStreaming = [bool]$_."is-streaming";
        }
    }
}

################################################################################
### ----------------------------------------------------------------------------
### Get-NaSystemApiTypes -server [NaServer]
###   Returns a list of typedefs for OnTAPI.
###
###   Example:
###            $server = New-NaServer -server "TOASTER" -credentials (Get-Credential)
###            Get-NaSystemApiTypes -server $server
###
################################################################################
function Get-NaSystemApiTypes {
    param (
        [PSObject] $server = $(throw "The parameter -server is required.")
    )
    
    $request = New-NaRequest -name "system-api-list-types"
    $result  = Get-NaResult -server $server -request $request
    
    if (!(Test-NaResult -result $result)) {
        Throw-NaResultError -result $result
    }
    
    return $result."type-entries"."system-api-type-entry-info" |% {
        New-NaObject -properties @{
            Name     = $_.name;
            Elements = $_."type-elements"."system-api-element-info" |% { ConvertTo-NaSystemApiElementInfo $_ };
        }
    }
}

################################################################################
### ----------------------------------------------------------------------------
### Get-NaSystemAvailableReplicationTransfers -server [NaServer]
###   Returns the number of replication operations that could be started and the maximum number of transfers for each replication type.
###
###   Example:
###            $server = New-NaServer -server "TOASTER" -credentials (Get-Credential)
###            Get-NaSystemAvailableReplicationTransfers -server $server
###            
################################################################################
function Get-NaSystemAvailableReplicationTransfers {
    param (
        [PSObject] $server = $(throw "The parameter -server is required.")
    )
    
    $request = New-NaRequest -name "system-available-replication-transfers"
    $result  = Get-NaResult -server $server -request $request
    
    if (!(Test-NaResult -result $result)) {
        Throw-NaResultError -result $result
    }
    
    return $result."replication-transfer-table"."replication-transfer-info" |% {
        New-NaObject -properties @{
            Type               = $_."replication-type";
            AvailableTransfers = [Int64]$_."replication-available-transfers";
            MaximumTransfers   = [Int64]$_."replication-maximum-transfers";
        }
    }
}

################################################################################
### ----------------------------------------------------------------------------
###  Get-NaSystemInfo -server [NaServer]
###   Returns the appliance information which includes cpu and backplane information.
###
###   Example:
###            $server = New-NaServer -server "TOASTER" -credentials (Get-Credential)
###            Get-NaSystemInfo -server $server
###            
################################################################################
function Get-NaSystemInfo {
    param (
        [PSObject] $server = $(throw "The parameter -server is required.")
    )
    
    $request = New-NaRequest -name "system-get-info"
    $result  = Get-NaResult -server $server -request $request
    
    if (!(Test-NaResult -result $result)) {
        Throw-NaResultError -result $result
    }
    
    return $result."system-info" |% {
        New-NaObject -properties @{
            BackplanePartNumber       = $_."backplane-part-number";
            BackplaneRevision         = $_."backplane-revision";
            BackplaneSerialNumber     = $_."backplane-serial-number";
            BoardSpeed                = $_."board-speed";
            BoardType                 = $_."board-type";
            ControllerAddress         = $_."controller-address";
            CpuCiobRevisionId         = $_."cpu-ciob-revision-id";
            CpuFirmwareRelease        = $_."cpu-firmware-release";
            CpuMicrocodeVersion       = $_."cpu-microcode-version";
            CpuPartNumber             = $_."cpu-part-number";
            CpuProcessorId            = $_."cpu-processor-id";
            CpuProcessorType          = $_."cpu-processor-type";
            CpuRevision               = $_."cpu-revision";
            CpuSerialNumber           = $_."cpu-serial-number";
            MemorySize                = [int]$_."memory-size";
            NumberOfProcessors        = [int]$_."number-of-processors";
            PartnerSystemId           = $_."partner-system-id";
            PartnerSystemName         = $_."partner-system-name";
            PartnerSystemSerialNumber = $_."partner-system-serial-number";
            ProdType                  = $_."prod-type";
            SupportsRaidArray         = [bool]$_."supports-raid-array";
            SystemId                  = $_."system-id";
            SystemMachineType         = $_."system-machine-type";
            SystemModel               = $_."system-model";
            SystemName                = $_."system-name";
            SystemRevision            = $_."system-revision";
            SystemSerialNumber        = $_."system-serial-number";
            VendorId                  = $_."vendor-id";
        }
    }
}

################################################################################
### ----------------------------------------------------------------------------
### Get-NaSystemOnTAPIVersion -server [NaServer]
###   Returns the current OnTAPI version of the server.
###
###   Example:
###            $server = New-NaServer -server "TOASTER" -credentials (Get-Credential)
###            $server.Version = Get-NaSystemOnTAPIVersion -server $server
###            
################################################################################
function Get-NaSystemOnTAPIVersion {
    param (
        [PSObject] $server = $(throw "The parameter -server is required.")
    )
    
    $request = New-NaRequest -name "system-get-ontapi-version"
    $result  = Get-NaResult -server $server -request $request
    
    if (!(Test-NaResult -result $result)) {
        Throw-NaResultError -result $result
    }
    
    return ("{0}.{1}" -f $result."major-version",$result."minor-version")
}

################################################################################
### ----------------------------------------------------------------------------
### Get-NaSystemVendorInfo -server [NaServer]
###   Returns the Data OnTAP vendor information.
###
###   Example:
###            $server = New-NaServer -server "TOASTER" -credentials (Get-Credential)
###            Get-NaSystemVendorInfo -server $server
###            
################################################################################
function Get-NaSystemVendorInfo {
    param (
        [PSObject] $server = $(throw "The parameter -server is required.")
    )
    
    $request = New-NaRequest -name "system-get-vendor-info"
    $result  = Get-NaResult -server $server -request $request
    
    if (!(Test-NaResult -result $result)) {
        Throw-NaResultError -result $result
    }
    
    return $result |% {
        New-NaObject -properties @{
            AutosupportEmail       = $_."autosupport-email";
            AutosupportUrl         = $_."autosupport-url";
            CompleteName           = $_."complete-name";
            CustomerSupportContact = $_."customer-support-contact";
            CustomerSupportName    = $_."customer-support-name";
            InformationUrl         = $_."information-url";
            OnTapOidPrefix         = $_."ontap-oid-prefix";
            ProductUrl             = $_."product-url";
            ShortName              = $_."short-name";
        }
    }
}

################################################################################
### ----------------------------------------------------------------------------
### Get-NaSystemVersion -server [NaServer]
###   Returns the Data OnTAP version of the server.
###
###   Example:
###            $server = New-NaServer -server "TOASTER" -credentials (Get-Credential)
###            Get-NaSystemVersion -server $server
###            
################################################################################
function Get-NaSystemVersion {
    param (
        [PSObject] $server = $(throw "The parameter -server is required.")
    )
    
    $request = New-NaRequest -name "system-get-version"
    $result  = Get-NaResult -server $server -request $request
    
    if (!(Test-NaResult -result $result)) {
        Throw-NaResultError -result $result
    }
    
    return $result.version
}

################################################################################
### ----------------------------------------------------------------------------
### ConvertTo-NaSystemApiElementInfo -inputObject [System.Xml.XmlNode]
###   Converts a XML node to a [NaSystemApiElementInfo] object.
###
###   Example:
###            $server  = New-NaServer -server "TOASTER" -credentials (Get-Credential)
###            $request = New-NaRequest -name "system-api-list-types"
###            $result  = Get-NaResult -server $server -request $request
###
###            if (!(Test-NaResult -result $result)) {
###              Throw-NaResultError -result $result
###            }
###
###            $elements = $result."type-entries"."system-api-type-entry-info" |% {
###              $_."type-elements"."system-api-element-info" |% { 
###                ConvertTo-NaSystemApiElementInfo $_
###              };
###            }
###            
################################################################################
function ConvertTo-NaSystemApiElementInfo {
    param (
        [System.Xml.XmlNode] $inputObject = $(throw "The parameter -inputObject is required.")
    )
    
    if ($inputObject -ne $null) {    
        return $inputObject |% {
            New-NaObject -properties @{
                Name        = $_.name;
                Type        = $_.type;
                Encrypted   = [bool]$_.encrypted;
                IsNonEmpty  = [bool]$_."is-nonempty";
                IsOptional  = [bool]$_."is-optional";
                IsOutput    = [bool]$_."is-output";
                IsValidated = [bool]$_."is-validated";
            }
        }
    }
}